package org.lztvn.elearning;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
public class ConfirmationMail {
 
    public static void main(String[] args) {
 
        int id=0;
        String passWord=null;
        sendMail(id, passWord);
    }
 
        public static void sendMail(int id, String passWord) {
        // TODO Auto-generated method stub
 
        final String username = "";
        final String password = "xxxxx";
        final String host= "smtp.gmail.com";
        final int port=465;
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
 
        Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
          });
 
        try {
 
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("nguyen.manhdung33@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("nguyen.manhdung33@gmail.com"));
            message.setSubject("Testing Subject");
            message.setText("Dear Mail Crawler,"
                + "\n\n No spam to my email, please!");
 
            Transport transport = session.getTransport("smtps");
            transport.connect (host, port,username,password);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();  
 
            System.out.println("Message sent successfully");
 
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}